<?php
	class Storage_product_history_model extends CI_Model {
	       
	var $id;
	var $storage_id;
	var $log;
	var $quantity;
	var $created_date;
	var $active;

    var $history_table;

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();

        $this->history_table = 'history';
    }

    // load all history [import]
    function loadAll()
    {
        $this->db->select('*');
        $this->db->from('storage_product_history');        
        $this->db->order_by("id", "desc"); 
        $query = $this->db->get();
        return $query->result();
    }   

    function getEntry($con,$id)
    {
        if($con == 'id')
        {
            $query = $this->db->get_where('storage_product_history', array('id' => $id));
        }
        else if($con == 'storage_id')
        {
            $query = $this->db->get_where('storage_product_history', array('storage_id' => $id));
        }
        
        return $query->result();
    }

    function insert($data)
    {
        $this->db->insert('storage_product_history',$data);
        return $this->db->insert_id();
    }

    //////////////////////////// history table ////////////////////////////    
    function loadHistory($type = false,$search = false)
    {
        $this->db->select('*');
        $this->db->from($this->history_table);        
        if(!empty($type))
        {
            $this->db->where('type',$type);
        }
        if(!empty($search))
        {
            $this->db->where('date_created >',$search);
            $next_date = date('Y-m-d',strtotime('+1 day',strtotime($search)));
            $this->db->where('date_created <',$next_date);
        }
        $this->db->order_by("id", "desc"); 
        $query = $this->db->get();
        return $query->result();
    }

    function loadHistoryForPrintExcel($type = false,$cond = false)
    {
        $this->db->select('*');
        $this->db->from($this->history_table);        
        if(!empty($type))
        {
            $this->db->where('type',$type);
        }
        if(!empty($cond))
        {
            if(!empty($cond['from_date']))
                $this->db->where('date_created >',$cond['from_date']);    
            if(!empty($cond['to_date']))
                $this->db->where('date_created <',$cond['to_date']);    
            else
            {                
                $next_date = date('Y-m-d',strtotime('+1 day',strtotime($cond['from_date'])));            
                $this->db->where('date_created <',$next_date);    
            }
        }
        $this->db->order_by("id", "desc"); 
        $query = $this->db->get();
        return $query->result();
    }

    function loadHistoryV2($type = false,$search = false,$limit = false, $start = false)
    {
        $this->db->select('*');
        $this->db->from($this->history_table);        
        if(!empty($type))
        {
            $this->db->where('type',$type);
        }
        if(!empty($search))
        {
            $this->db->where('date_created >',$search);
            $next_date = date('Y-m-d',strtotime('+1 day',strtotime($search)));
            $this->db->where('date_created <',$next_date);
        }
        $this->db->order_by("id", "desc"); 
        
        $this->db->limit($limit, $start);
        
        $query = $this->db->get();        

        $return = array(
                'data'  => $query->result(),
                'count' => $this->countHistory($type, $search), 
            );
        return $return;
    }

    function loadHistoryV3($type = false,$cond = false,$limit = false, $start = false)
    {
        $this->db->select('*');
        $this->db->from($this->history_table);        
        if(!empty($type))
        {
            $this->db->where('type',$type);
        }
        if(!empty($cond))
        {
            if(!empty($cond['from_date']))
                $this->db->where('date_created >',$cond['from_date']);    
            if(!empty($cond['to_date']))
                $this->db->where('date_created <',$cond['to_date']);    
            else
            {                
                $next_date = date('Y-m-d',strtotime('+1 day',strtotime($cond['from_date'])));            
                $this->db->where('date_created <',$next_date);    
            }
        }
        $this->db->order_by("id", "desc"); 
        
        $this->db->limit($limit, $start);
        
        $query = $this->db->get();             

        $return = array(
                'data'  => $query->result(),
                'count' => $this->countHistoryV2($type, $cond), 
            );
        return $return;
    }

    function countHistory($type = false,$search = false)
    {
        $this->db->select('*');
        $this->db->from($this->history_table);        
        if(!empty($type))
        {
            $this->db->where('type',$type);
        }
        if(!empty($search))
        {
            $this->db->where('date_created >',$search);
            $next_date = date('Y-m-d',strtotime('+1 day',strtotime($search)));
            $this->db->where('date_created <',$next_date);
        }
        
        return $this->db->count_all_results();
    }

    function countHistoryV2($type = false,$cond = false)
    {
        $this->db->select('*');
        $this->db->from($this->history_table);        
        if(!empty($type))
        {
            $this->db->where('type',$type);
        }
        if(!empty($cond))
        {
            if(!empty($cond['from_date']))
                $this->db->where('date_created >',$cond['from_date']);    
            if(!empty($cond['to_date']))
                $this->db->where('date_created <',$cond['to_date']);    
            else
            {                
                $next_date = date('Y-m-d',strtotime('+1 day',strtotime($cond['from_date'])));            
                $this->db->where('date_created <',$next_date);    
            }
        }
        
        return $this->db->count_all_results();
    }

    function insertHistory($data)
    {
        $this->db->insert($this->history_table,$data);
        return $this->db->insert_id();
    }

    function updateHistory($data, $id)
    {
        $this->db->where('id',$id);
        $this->db->update($this->history_table,$data);
    }

    ///////////////////////////////////////////////////////////////////////
}